package jp.co.toshiba.ffsa.action.csas;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.form.csas.Csas501Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csas.Csas501Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * (パスワード忘れ)ID入力
 * @author npc
 */
public class Csas501Action extends CommonAction {
    /** 画面ID */
    public String screenID = "CSAS501";

    @ActionForm
    @Resource
    protected Csas501Form csas501Form;

    @Resource
    protected Csas501Service csas501Service;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception エラー
     */
    @Execute(validator = false)
    public String index() throws Exception {
        // リファラ判定
        if(this.judgeRefferer()){
            // セッションタイムアウト判定
            if(this.judgeSession()){
                // 遷移先
                String jspName = "/csas/csas501.jsp";
                return jspName;
            }else{
                return this.topTransition;
            }
        }else{
            return this.topTransition;
        }
    }

    /**
     * 仮パスワード発行処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true, input="/csas/csas501/error")
    public String issuingProcess () throws Exception{

        String transition = "/csas/csas501.jsp"; // 遷移先
        String result = "";

        try{
            // リファラ判定
            if(this.judgeRefferer()){
                // セッションタイムアウト判定
                if(this.judgeSession()){
                    csas501Service = new Csas501Service();
                    csas501Service.setEmailAddress(this.csas501Form.emailAddress);
                    csas501Service.setFirstName(this.csas501Form.firstName);
                    csas501Service.setLastName(this.csas501Form.lastName);
                    Boolean bFlag = csas501Service.checkUserPresence();

                    if(bFlag){
                        // 仮パスワード発行
                        if ("".equals(csas501Service.tmpPasswordIssued()) ){
                            //エラー表示
                            ActionMessages messages = new ActionMessages();
                            messages.add("tmpPasswordIssuedFailed", new ActionMessage("csas501.902", true));
                            ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                            transition = "/csas/csas101.jsp";
                            result = ConstantValue.fail;
                        } else {
                            // メール送信
                            if (!csas501Service.mailSend() ){
                                //エラー画面に遷移
                                ActionMessages messages = new ActionMessages();
                                messages.add("mailSendFailed", new ActionMessage("csas501.903", true));
                                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                                transition = "/csas/csas101.jsp";
                                result = ConstantValue.fail;
                            } else {
                                transition = "/csas/csas502.jsp";
                                result = ConstantValue.success;
                            }
                        }

                        if(ConstantValue.success.equals(result)){
                            // 操作ログ記述
                            ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                            ArrayList<String> parameterNames = new ArrayList<String>();
                            ArrayList<String> values = new ArrayList<String>();
                            parameterNames.add(MessageResourcesUtil.getMessage("mail_address"));
                            values.add("-");
                            parameterNames.add(MessageResourcesUtil.getMessage("first_name"));
                            values.add(this.csas501Form.firstName);
                            parameterNames.add(MessageResourcesUtil.getMessage("last_name"));
                            values.add(this.csas501Form.lastName);
                            parameters.add(parameterNames);
                            parameters.add(values);
                            this.entryOperationHistory(this.screenID, "submit", parameters, result, csas501Service.getUserID());
                        }

                        return transition;

                   }else{
                        // 該当データがない場合
                        ActionMessages messages = new ActionMessages();
                        messages.add("issuingProcessFailed", new ActionMessage("csas501.901", true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                        transition = "/csas/csas501.jsp";
                    }
                }else{
                    return this.topTransition;
                }
            }else{
                return this.topTransition;
            }
        }catch(Exception e){
            this.errorLogWriter(e, "issuingProcess", "CSAS501");
            throw e;
        }
        return transition;
    }

    /**
     * バリデーションエラー処理
     * @return 遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String error() throws Exception{
        // リファラ判定
        if(this.judgeRefferer()){
            // セッションタイムアウト判定
            if(this.judgeSession()){
                return index();
            }else{
                return this.topTransition;
            }
        }else{
            return this.topTransition;
        }
    }
}